package com.koron.housing.apply.controller;


import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.housing.apply.bean.dto.HousingStaffApplyDTO;
import com.koron.housing.apply.bean.po.HousingStaffIntegral;
import com.koron.housing.apply.bean.query.HousingApplyQuery;
import com.koron.housing.apply.bean.vo.HousingStaffApplyVO;
import com.koron.housing.apply.service.HousingStaffApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 需求申请(HousingStaffApply)表控制层
 *
 * @author jzp
 * @since 2022-02-26 15:06:02
 */
@Api(tags = "需求申请接口")
@RestController
@RequestMapping("housingStaffApply")
public class HousingStaffApplyController implements WorkflowController {
    /**
     * 服务对象
     */
    @Autowired
    private HousingStaffApplyService housingStaffApplyService;

    @Autowired
    private WorkFlowHandler workFlowHandler;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<HousingStaffApplyDTO> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "getDtoById", HousingStaffApplyDTO.class, id));
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("page")
    public Response<PageInfo<HousingStaffApplyVO>> page(PageQuery pageQuery, HousingApplyQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "page", PageInfo.class, pageQuery, query));
    }

    @ApiOperation(value = "保存或更新")
    @PostMapping("save")
    public Response<String> save(@RequestBody HousingStaffApplyDTO housingStaffApplyDTO) {
        ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "saveDto", Boolean.class, housingStaffApplyDTO);
        return Response.ok(housingStaffApplyDTO.getId());
    }

    @ApiOperation(value = "保存或更新积分信息")
    @PostMapping("saveIntegral")
    public Response<String> saveIntegral(@RequestBody HousingStaffIntegral housingStaffIntegral) {
        ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "saveIntegral", Boolean.class, housingStaffIntegral);
        return Response.ok(housingStaffIntegral.getId());
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "deleteById", Boolean.class, id);
        workFlowHandler.delete(id);
        return Response.ok();
    }

    /**
     * 工作溜
     */
    @Override
    public Response<Object> updateState(@RequestBody RedirectQuery redirectQuery) {
        // 提交
        if (redirectQuery.getOperation() == 32) {
            // 如果是结束
            if (redirectQuery.getCurNodeCode().equals("END")) {
                ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "end", Boolean.class, redirectQuery.getDataId());
            } else {
                // 否则为提交
                ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "submit", Boolean.class, redirectQuery.getDataId());
            }
        } else if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            // 如果是驳回或撤回，回到草稿状态
            ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "updateStatus", Boolean.class, redirectQuery.getDataId(), "0");
        } else if (redirectQuery.getOperation() == 16) {
            // 如果是废弃，状态改为草稿，并删除流程
            ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "updateStatus", Boolean.class, redirectQuery.getDataId(), "0");
            workFlowHandler.delete(redirectQuery.getDataId());
        }
        return Response.success();
    }

    @ApiOperation(value = "统一提交并结束")
    @PostMapping("submitAndEnd")
    public Response<String> submitAndEnd(@RequestBody List<String> ids) {
        for (String id : ids) {
            ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "submit", Boolean.class, id);
            ADOConnection.runTask(getCurrDs(), housingStaffApplyService, "end", Boolean.class, id);
        }
        return Response.ok();
    }

}
